package com.android.lyricshow.data;

import java.util.*;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

import com.android.lyricshow.bean.AlbumBean;
import com.android.lyricshow.bean.ArtistBean;
import com.android.lyricshow.bean.SongBean;

public class DataProvider {
	
	private static final String[] SONG_SELECTION	= new String[]{MediaStore.Audio.Media._ID,
		MediaStore.Audio.Media.TITLE,
		MediaStore.Audio.Media.ALBUM,
		MediaStore.Audio.Media.ARTIST,
		MediaStore.Audio.Media.DURATION,
		MediaStore.Audio.Media.DATA,
		MediaStore.Audio.Media.ALBUM_ID};
	
	private static final String[] ABLUM_SELECTION	= new String[]{
		MediaStore.Audio.Albums._ID,
		MediaStore.Audio.Albums.ALBUM,
		MediaStore.Audio.Albums.ARTIST,
	};
	
	private static final String[] ARTIST_SELECTION	= new String[]{
		MediaStore.Audio.Artists._ID,
		MediaStore.Audio.Artists.ARTIST
	};
	
	private Context mContext = null;
	
	public DataProvider(Context context){
		mContext = context;
	}
	
	public ArrayList<ArtistBean> getArtistList(){ 
		
		ArrayList<ArtistBean> artistlist = new ArrayList<ArtistBean>();
		
		Cursor cr = mContext.getContentResolver().query(
				MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI,
				ARTIST_SELECTION, 
				null,
				null, 
				null);

		if(cr == null){ return artistlist; }
		
		while(cr.moveToNext()){
			long id 		= cr.getLong(0);
			String artist	= cr.getString(1);
			
			ArtistBean abean = new ArtistBean();
			
			abean.setID(id);
			abean.setName(artist);
			
			artistlist.add(abean);
		}
		
		cr.close();
		
		return artistlist;
	}
	
	public ArrayList<AlbumBean> getAlbumList(){ 
		
		ArrayList<AlbumBean> ablumlist = new ArrayList<AlbumBean>();
		
		Cursor cr = mContext.getContentResolver().query(
				MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
				ABLUM_SELECTION, 
				null,
				null, 
				null);

		if(cr == null){ return ablumlist; }
		
		while(cr.moveToNext()){
			long id 		= cr.getLong(0);
			String album 	= cr.getString(1);
			String artist	= cr.getString(2);
			
			AlbumBean abean = new AlbumBean();
			
			abean.setID(id);
			abean.setName(album);
			abean.setArtist(artist);
			
			ablumlist.add(abean);
		}
		
		cr.close();
		
		return ablumlist;
	}
	
	public ArrayList<SongBean> getSongList(){

		ArrayList<SongBean> songlist = new ArrayList<SongBean>();
		
		Cursor cr = mContext.getContentResolver().query(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
				SONG_SELECTION, 
				MediaStore.Audio.Media.IS_MUSIC + "<>?",
				new String[]{0 + ""}, 
				null);

		if(cr == null){ return songlist; }
		
		while(cr.moveToNext()){
			
			long id 		= cr.getLong(0);
			
			String title	= cr.getString(1);
			String album 	= cr.getString(2);
			String artist	= cr.getString(3);
			long duration	= cr.getLong(4);
			String path		= cr.getString(5);
			long albumid	= cr.getLong(6);
			
			SongBean sbean = new SongBean();
			
			sbean.setID(id);
			sbean.setTitle(title);
			sbean.setArtist(artist);
			sbean.setPath(path);
			sbean.setAlbum(album);
			sbean.setTime(duration);
			sbean.setAlbumID(albumid);
			
			songlist.add(sbean);
		}
		
		cr.close();

		return songlist;
	}
}
